package edu.uwosh.cs342.project3;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class ViewScores extends Activity {
	
	String[] quizNames;
	int[] quizScores;
	float average;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewallscores);
		
		instantiateDummyData();
		
		TableLayout scoresTable = (TableLayout) findViewById(R.id.scoresLayout);
		
		showScores(scoresTable);
		
		showAverageAndStandardDev(scoresTable);
	}
	
	public void topOfTable(TableLayout t){
		addSpaceToTable(t);
		
		addScoresLabel(t);
		
		addSpaceToTable(t);
	}
	
	public void addScoresLabel(TableLayout t){
		
		TableRow tr = new TableRow(this);
		tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
        
		TextView scoreTextView = new TextView(this);
        	scoreTextView.setText("             Quiz Name                           Quiz Score               " );
        	scoreTextView.setTextSize(25);
		
        tr.addView(scoreTextView);
        
        t.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
	}
	
	public void addSpaceToTable(TableLayout t){
		
		TableRow tr = new TableRow(this);
		tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
        
		TextView scoreTextView = new TextView(this);
        	scoreTextView.setText("                 ");
        	scoreTextView.setTextSize(25);
		
        tr.addView(scoreTextView);
        
        t.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
	}
	
	public void showScores(TableLayout t){
		
		//  Title of table
		topOfTable(t);
		
		//  Showing of quiz names and scores
		for(int i = 0; i < quizNames.length; i++)
		{TableRow tr = new TableRow(this);
		tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
        
		TextView scoreTextView = new TextView(this);
        	scoreTextView.setText("                " + quizNames[i] + "                                       " 
        						+ quizScores[i]);
        	scoreTextView.setTextSize(25);
		
        tr.addView(scoreTextView);
        
        t.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
		}
	}

	public void showAverageAndStandardDev(TableLayout t){
		addSpaceToTable(t);
		averageAndStandardDevTitle(t);
		addSpaceToTable(t);
		
		TableRow tr = new TableRow(this);
		tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
        
		TextView scoreTextView = new TextView(this);
        	scoreTextView.setText("                        Average:           " + average + "              " );
        					   // "             Standard Deviation:    "
        	scoreTextView.setTextSize(25);
		
        tr.addView(scoreTextView);
        
        t.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        
        calculateAndShowStdDev(t);
	}
	
	public void calculateAndShowStdDev(TableLayout t){
		
		int stdDev = 0;
		float devSum = 0;
		
		for(int i=0; i < quizScores.length; i++){
			devSum = devSum + ((average - (float)quizScores[i]) * (average - (float)quizScores[i]));
		}
		
		double endResult = Math.sqrt((devSum/quizScores.length));
		
		
		TableRow tr = new TableRow(this);
		tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
        
		TextView scoreTextView = new TextView(this);   
        	scoreTextView.setText("             Standard Deviation:    " + endResult + "              " );
        	scoreTextView.setTextSize(25);
		
        tr.addView(scoreTextView);
        
        t.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
	}
	
	public void averageAndStandardDevTitle(TableLayout t){
		addSpaceToTable(t);
		
		TableRow tr = new TableRow(this);
		tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
        
		TextView scoreTextView = new TextView(this);
        	scoreTextView.setText("Your average and standard deviation:              " );
        	scoreTextView.setTextSize(35);
		
        tr.addView(scoreTextView);
        
        t.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
	}
	
	public void instantiateDummyData(){
		quizNames = new String[]{"quiz1", "quiz2", "quiz3", "quiz4", "quiz5"};
		
		quizScores = new int[]{85, 90, 96, 100, 70};
		
		average = (float) 88.2;
	}

}
